package com.pw.study.flink.cep;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;

/**
 * @Author: linux_future
 * @since: 2022/3/20
 **/
public abstract class BaseCEP {
    public void init() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2055);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        WatermarkStrategy<JSONObject> wms = WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner((obj, ts) -> obj.getLong("ts"));
        SingleOutputStreamOperator<JSONObject> stream = env.readTextFile("data/file/app.2022-03-16.log")
                .map(JSON::parseObject).assignTimestampsAndWatermarks(wms);
        handler(env, stream);
        //输入数据

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected abstract void handler(StreamExecutionEnvironment env, SingleOutputStreamOperator<JSONObject> stream);

}
